<?php
header("Content-Type: text/html; charset=iso-8859-1");
require 'include.php';
$user_id = check_cookie();
if(!check_admin("commission_arb_marq")) redirect('perso.php?action=admin');
else{
	$_SESSION['navigation'] = "marquages_admin";
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "list";
	$match_no = (!empty($_REQUEST['match_no'])) ? $_REQUEST['match_no'] : "";
	$liste_marqueurs = "<option value = '' style = 'color: #C00'>---> aucun marqueur <---</option>";
	$q = "select id from membres where marqueur = 1 and valide = 1 order by prenom,nom";
	$r = ExecRequete($q);
	while($s = LigneSuivante($r)){
		$marqueur = new membre($s->id);
		$liste_marqueurs .= "<option value = '$s->id'>$marqueur->prenom $marqueur->nom (".$marqueur->nb_marquages.")</option>";
	}
	if($action == 'update'){
		$option = (!empty($_REQUEST['option'])) ? $_REQUEST['option'] : "prochains";
		$_SESSION['affichage'] = $option;
	}
	elseif($action == 'detail'){
		$match = new match($match_no);
		$salle = new salle($match->salle);
		$date = format_date($match->date);
		$heure = format_heure($match->heure);
		$fields = describe('matchs');
		echo "<div>\n";
		echo "<h2>Marquage $match_no</h1>";
		foreach($fields as $field => $type){
			echo leftCol("<span class = 'bold'>$field</span>");
			if($field == 'date'){
				$print = $date;
			}
			elseif($field == 'heure'){
				$print = $heure;
			}
			elseif($field == 'salle'){
				$print = $salle->nom;
			}
			elseif(strpos($field,'marqueur') !== FALSE){
				$membre = new membre($match->$field);
				$print = $membre->prenom." ".$membre->nom;
			}
			else $print = $match->$field;
			echo rightCol($print);
		}
		echo "<br class = 'clear'/>\n";
		if($match->date >= date('Y-m-d')){
			echo "<h2>autres marqueurs disponibles</h2>\n";
			$bonnes = array();
			$q = "select distinct(equipe) from matchs where date = '$match->date' and equipe != '$match->equipe' and salle = '$match->salle'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$equipe = new equipe($s->equipe,"nom");
				$bonnes[$equipe->id] = $s->equipe;
			}
			$q = "select equipe_id,nom from entrainements left join equipes on equipe_id = equipes.id where jour = '$match->jour' and salle = '$match->salle' and nom != '$match->equipe'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$bonnes[$s->equipe_id] = $s->nom;
			}
			$equipes = liste('equipes');
			$del = new equipe($match->equipe,"nom");
			unset($equipes[$del->id]);
			$q = "select distinct(equipe) from matchs where date = '$match->date' and salle != '$match->salle'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				if(isset($equipes)){
					foreach($equipes as $id => $nom){
						if($nom == $s->equipe){unset($equipes[$id]); }
					}
				}
			}
			if(isset($equipes)){
				$q = "select equipe_id,nom from entrainements left join equipes on equipe_id = equipes.id where jour = '$match->jour' and salle != '$match->salle' and nom != '$match->equipe'";
				$r = ExecRequete($q);
				while($s = LigneSuivante($r)){
					if(isset($equipes)){
						foreach($equipes as $id => $nom){
							if($nom == $s->nom){unset($equipes[$id]); }
						}
					}
				}
			}
			$categories = array("Bonnes disponibilit&eacute;s" => "bonnes", "Disponibles" => "equipes");
			foreach($categories as $titre => $table){
				echo "<p class = 'bold center'>$titre</p>";
				$i = 0;
				${"print_membres_".$table}= array();
				if(isset($$table)){
					foreach($$table as $id => $nom){
						$equipe = new equipe($id);
						$membres = $equipe->list_membres();
						foreach($membres as $membre_id => $membre){
							$ok = ($membre->marqueur) ? 1 : 0;
							if($ok){
								$marquages = $membre->marquages;
								foreach($marquages as $marq_id => $marq_no){
									$marquage = new match($marq_no);
									if($marquage->date == $match->date) $ok = 0;
								}
								// $arbitrages = $membre->arbitrages;
								// foreach($arbitrages as $marq_id => $match_no){
								// 	$arbitrage = new match($match_no);
								// 	if($arbitrage->date == $match->date) $ok = 0;
								// }
							}
							if($ok){
								${"print_membres_".$table}[$membre->id] = $membre->prenom." ".$membre->nom." ($membre->nb_marquages)";
								if($membre->nb_marquages == "0" || $membre->nb_marquages == "1") ${"print_membres_".$table}[$membre->id] = "<span class = 'highlight'>".${"print_membres_".$table}[$membre->id]."</span>";
							}
						}
					}
					asort(${"print_membres_".$table});
					foreach(${"print_membres_".$table} as $membre_id => $print){
						if($table == 'bonnes' || !array_key_exists($membre_id,$print_membres_bonnes)){
							$print .=
							$echo = "<span style = 'cursor: pointer' onclick = 'show_membre($membre_id,\"$match_no\")'>$print</span>";
							if(++$i % 2) echo leftCol($echo);
							else echo rightCol($echo);
						}
					}
					if(!count(${"print_membres_".$table})) echo "<p class = 'center fade'>aucune</p>\n";
					if($i % 2) echo rightCol("");
					echo "<br class = 'clear' />";
				}
			}
		}
		else echo "<p class = 'center fade'>La date du marquage est pass&eacute;e</p>\n";
		echo "</div>\n";
	}
	elseif($action == 'confirm'){
		$match_no = (!empty($_REQUEST['match_no'])) ? $_REQUEST['match_no'] : "";
		$match = new match($match_no);
		$salle = new salle($match->salle);
		$date = format_date($match->date);
		$heure = format_heure($match->heure);
		$jour = $match->jour;
		$equipe = $match->equipe;
		$user_id = (!empty($_REQUEST['membre_id'])) ? $_REQUEST['membre_id'] : "";
		$field = (!empty($_REQUEST['field'])) ? $_REQUEST['field'] : "";
		if(!$user_id){
			echo "<p class = 'center bold'>Confirmer la d&eacute;sinscription au marquage du $date &agrave; $heure &agrave; $salle->nom</p>";
			echo "<p class = 'center'><button onclick = 'add_marquage(\"$match_no\",\"$field\",\"\",1);print_marquages(1);'>confirmer</button><button onclick = 'center_box(0);print_marquages(1)'>annuler</button></p>";
		}
		else{
			$membre = new membre($user_id);
			$matchs = $membre->matchs();
			$message = "";
			echo "<p class = 'center bold'>$membre->prenom $membre->nom</p>\n";
			foreach($membre->marquages as $match){
				if(format_date($match->date) == $date){
					$message .= "<p class = 'center bold'>!! Attention, $membre->prenom $membre->nom a un marquage le $date !!</p>\n";
				}
			}
			if(!$message){
				foreach($matchs as $match){
					if(format_date($match->date) == $date){
						$message .= "<p class = 'center bold'>!! Attention, $membre->prenom $membre->nom a un match le $date !!</p>\n";
					}
				}
			}
			if(in_array($equipe,$membre->equipes)){
				$message .= "<p class = 'center bold'>!! Attention, $membre->prenom $membre->nom fait partie de l'&eacute;quipe en jeu !!</p>\n";
			}
			$q = "select jour, salle, heure, membre_equipes.equipe_id as equipe_id from membre_equipes left join entrainements on membre_equipes.equipe_id = entrainements.equipe_id where membre_id = '$membre->id'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				if($jour == $s->jour && $salle != $s->salle){
					$message .= "<p class = 'center bold'>!! Attention, $membre->prenom $membre->nom a un entra&icirc;nement en m&ecirc;me temps !!</p>";
				}
			}
			if($message) echo $message;
			echo "<p class = 'center bold'>Confirmer l'inscription au marquage du $date &agrave; $heure &agrave; $salle->nom</p>";
			echo "<p class = 'center'><button onclick = 'add_marquage(\"$match_no\",\"$field\",$user_id,1);print_marquages(1);'>confirmer l'inscription</button><button onclick = 'center_box(0);print_marquages(1)'>annuler</button></p>";
		}
	}
	elseif($action == 'add'){
		$match_no = (!empty($_REQUEST['match_no'])) ? $_REQUEST['match_no'] : "";
		$user_id = (!empty($_REQUEST['membre_id'])) ? $_REQUEST['membre_id'] : "";
		$field = (!empty($_REQUEST['field'])) ? $_REQUEST['field'] : "";
		if($user_id){
			$match = new match($match_no);
			$membre = new membre($user_id);
			if($membre->marqueur){
				if($match->marqueur1){
					$membre = new membre($match->marqueur1);
					$ddns = explode("-",$membre->ddn);
					$year = $ddns[0];
					if(date('Y') - $year < 20){
						$match->marqueur2 = $user_id;
						$match->update();
						mail_marquage($match->no);
						echo "Inscription réussie. Un email de confirmation a &eacute;t&eacute; envoy&eacute;.";
					}
					else{
						$match->marqueur1 = $user_id;
						$match->update();
						mail_marquage($match->no);
						echo "Inscription réussie. Un email de confirmation a &eacute;t&eacute; envoy&eacute;.";
					}
				}
				else{
					$match->marqueur1 = $user_id;
					$match->update();
					mail_marquage($match->no);
					echo "Inscription réussie. Un email de confirmation a &eacute;t&eacute; envoy&eacute;.";
				}
			}
			else echo "$membre->prenom $membre->nom doit &ecirc;tre marqueur pour s'inscrire";
		}
		else{
			$match = new match($match_no);
			$match->$field = '';
			$match->update();
			echo "D&eacute;sinscription réussie.\n";
		}
	}
	elseif($action == 'print'){
		$affichage = (!empty($_SESSION['affichage'])) ? $_SESSION['affichage'] : "";
		$equipes = liste("equipes");
		$table = new table();
		$table->filter = 0;
		$table->order = 'date';
		$table->styleclass = '';
		$fields = array("no","equipe","jour","date","heure","salle","marqueurs","info");
		foreach($fields as $field){
			$$field = new table_field($field);
			if($field == 'no') $no->hidden = 1;
			elseif(strpos($field,"marqu")!== FALSE) $$field->styleclass = 'nowrap';
			// elseif($field == 'info') $info->styleclass = 'center';
			$table->fields[] = $$field;
		}
		$id->hidden = 1;
		$marqueurs->styleclass = 'nowrap';
		$table->nb_rows = 30;
		$matchs = array();
		foreach($equipes as $id => $nom){
			$equipe = new equipe($id);
			$saison = "";
			$matchs = array_merge($matchs,$equipe->matchs($saison));
		#	$table->nb_rows += count($matchs);
		}
		$i = 0;
		foreach($matchs as $match){
			if((array_search($match->salle,$salles_lutry) !== FALSE || $match->marquage) && (($affichage == "prochains" && $match->date >= date('Y-m-d')) ||
				($affichage == "marquage" && !$match->marqueur1) ||
				($affichage == "tous"))){
					$row = new table_row();
					$row->onclick = "";
					foreach($fields as $field){
						if($field == 'salle'){
							$salle = new salle($match->$field);
							$echo = str_ireplace("salle","",$salle->nom);
						}
						elseif($field == 'info'){
							$caution = ($match->marqueur1) ? "" : "<img src = 'images/icon_warning.png' alt = '!!' height= '14' width = '14'/>";
							$echo = "<a href = 'javascript:detail_marquage(\"$match->no\",1); self.location.href = \"#top\"'><img src = 'images/icon_info.png' alt = 'i' height = '14' width = '14' style = \"cursor: pointer\" /></a>$caution<span id = 'ok_$match->no' class = 'oui hidden'>ok</span>";
						}
						elseif(strpos($field,'marqu') !== FALSE){
							if($match->marqueur1){
								$membre = new membre($match->marqueur1);
								$ddns = explode("-",$membre->ddn);
								$year = $ddns[0];
								if(date('Y') - $year < 20) $ok = 1;
								else $ok =0;
							}
							else $ok = 2;
							$j = ($ok == 1) ? "jun: " : "";
							$echo  = "1.<select name = 'marqueur1' onchange = \"confirm_marquage('$match->no','marqueur1',this.options[this.selectedIndex].value,1)\" style = 'font-size: 8pt;'>";
							$echo .= str_replace("<option value = '".$match->marqueur1."'>","<option value = '".$match->marqueur1."' selected = selected>$j",$liste_marqueurs);
							$echo .= "</select>";
							// if($match->date < date('Y-m-d')) $ok = 0;
							if($ok){
								$echo  .= "<br />2.<select name = 'marqueur2' onchange = \"confirm_marquage('$match->no','marqueur2',this.options[this.selectedIndex].value,1)\" style = 'font-size: 8pt;'>";
								$echo .= str_replace("<option value = '".$match->marqueur2."'>","<option value = '".$match->marqueur2."' selected = selected>",$liste_marqueurs);
								$echo .= "</select>";
							}
						}
						else $echo = str_ireplace("vbc ","",$match->$field);
						$row->cols[$field] = $echo;
					}
					$key = key($table->rows) + 1;
					$table->rows[] = $row;
					$i++;
			}
		}
		$footer = "";
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer($footer);
	}
	else{
		entete('marquages',"","",1);
		$_SESSION['affichage'] = 'prochains';
		$affichages = array("prochains" => "prochains marquages","tous" => "tous les marquages", "marquage" => "matchs sans marqueur");

		echo "<h1>Calendrier des marquages</h1>";
		echo "<p class = 'center bold'>options d'affichage : ";
		echo "<select name = 'affichage' onchange = 'update_marquages(this.options[this.selectedIndex].value,1)'>\n";
		foreach($affichages as $option => $print){
			$selected = ($_SESSION['affichage'] == $option) ? "selected = 'selected'" : "";
			echo "<option value = '$option' $selected>$print</option>\n";
		}
		echo "</select>\n";
		echo "</p>";
		echo "<div id = 'marquages'>\n";
		echo "</div>\n";
		echo "<script type = 'text/javascript'>print_marquages(1)</script>\n";
		pied();
	}
}

?>